package com.filebackup.cloudapi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import junit.framework.Assert;

import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.OSSException;
import com.aliyun.openservices.oss.model.AccessControlList;
import com.aliyun.openservices.oss.model.CannedAccessControlList;
import com.aliyun.openservices.oss.model.GetObjectRequest;
import com.aliyun.openservices.oss.model.Grant;
import com.aliyun.openservices.oss.model.OSSObjectSummary;
import com.aliyun.openservices.oss.model.ObjectListing;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.Permission;
import com.filebackup.daemon.DaemonConstants;

public class AliApi implements ICloudApi {

	private static OSSClient client;
	private static String accessId = "kaybe7g6r56hrf9gd1zbxf79";
	private static String accessKey = "JfXA+9H6UQd0z6FOZ+uIpAUDUj4=";
	private static String ossEndpoint = "http://storage.aliyun.com/";
	
	public AliApi() {
		client = new OSSClient(ossEndpoint, accessId, accessKey);
	}

	private void listObject(String bucketName) {

		try {
			ObjectListing objectListing = client.listObjects(bucketName);
			
			List<OSSObjectSummary> objlist = objectListing.getObjectSummaries();

			for (int i = 0; i < objlist.size(); i++) {
				String objectName = objlist.get(i).getKey();
				System.out.println("obj" + i + objectName);
			}

		} catch (OSSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void createBucket(String bucketName) {

		try {
			client.createBucket(bucketName);

			client.setBucketAcl(bucketName,
					CannedAccessControlList.PublicReadWrite);

			AccessControlList accessControlList = client
					.getBucketAcl(bucketName);
			
			Assert.assertEquals(Permission.FullControl,
					((Grant) (accessControlList.getGrants().toArray()[0]))
							.getPermission());
		} catch (OSSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void putObject(String bucketName, String uploadFile, String sourceFile) {
		
		Assert.assertNotNull(sourceFile);
		ObjectMetadata objectMeta = new ObjectMetadata();

		//objectMeta.addUserMetadata("Id", "123");
        //objectMeta.addUserMetadata("Name", "test");
        
        File file = new File(sourceFile);
        
        objectMeta.setContentLength(file.length()+1);

        InputStream input;
		try {
			input = new FileInputStream(file);
			client.putObject(bucketName, uploadFile, input, objectMeta);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OSSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}

	private void getObject(String bucketName, String objectName, String downloadFilePath) {
		try {
			client.getObject(new GetObjectRequest(bucketName, objectName), new File(downloadFilePath));
		} catch (OSSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void listFiles(String cloudPath) {
		this.listObject(cloudPath);
	}

	public void createPath(String cloudPath) {
		this.createBucket(cloudPath);
	}

	public void downloadFile(String cloudPath, String cloudFile,
			String savePath) {
		this.getObject(cloudPath, cloudFile, savePath);
	}

	public void uploadFile(String cloudPath, String cloudFile, String sourceFile) {
		this.putObject(cloudPath, cloudFile, sourceFile);
	}

    public String getApiName() {
        // TODO Auto-generated method stub
        return DaemonConstants.ALI_NAME;
    }

    public String getCloudPath() {
        // TODO Auto-generated method stub
        return DaemonConstants.ALI_PATH;
    }

}
